/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZEmailAddress;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.zclient.ZClientException;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestMaxMessageSize
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*  44 */   private static final String NAME_PREFIX = TestMaxMessageSize.class.getSimpleName();
/*     */   private static final long TEST_MAX_MESSAGE_SIZE = 2000L;
/*     */   private String mOrigMaxMessageSize;
/*     */   private String mOrigFileUploadMaxSize;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  52 */     cleanUp();
/*     */     
/*  54 */     Provisioning prov = Provisioning.getInstance();
/*  55 */     this.mOrigMaxMessageSize = prov.getConfig().getAttr("zimbraMtaMaxMessageSize", null);
/*  56 */     this.mOrigFileUploadMaxSize = prov.getLocalServer().getAttr("zimbraFileUploadMaxSize", null);
/*     */   }
/*     */   
/*     */   public void testMaxMessageSizeBelowThreshold() throws Exception
/*     */   {
/*  61 */     setMaxMessageSize(2000L);
/*  62 */     Map<String, byte[]> attachments = new HashMap();
/*  63 */     attachments.put("file1.exe", new byte['È']);
/*  64 */     attachments.put("file2.exe", new byte['Ĭ']);
/*  65 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  66 */     String aid = mbox.uploadAttachments(attachments, 5000);
/*  67 */     TestUtil.sendMessage(mbox, "user1", NAME_PREFIX, "Message size below threshold", aid);
/*     */   }
/*     */   
/*     */   public void testMaxMessageSizeAboveThreshold() throws Exception
/*     */   {
/*  72 */     setMaxMessageSize(2000L);
/*  73 */     Map<String, byte[]> attachments = new HashMap();
/*  74 */     attachments.put("file1.exe", new byte['̠']);
/*  75 */     attachments.put("file2.exe", new byte['ʼ']);
/*  76 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  77 */     String aid = mbox.uploadAttachments(attachments, 5000);
/*     */     try {
/*  79 */       TestUtil.sendMessage(mbox, "user1", NAME_PREFIX, "Message size above threshold", aid);
/*  80 */       fail("sendMessage() should not have succeeded");
/*     */     }
/*     */     catch (SoapFaultException e) {
/*  83 */       validateMessageTooBigFault(e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void testLimitByFileUploadMaxSize() throws Exception
/*     */   {
/*  89 */     setMaxMessageSize(2000L);
/*  90 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     try {
/*  92 */       InputStream in = new ByteArrayInputStream(new byte['ஸ']);
/*  93 */       mbox.uploadContentAsStream("testLimitByFileUploadMaxSize1", in, "application/octet-stream", 3000L, 5000);
/*  94 */       fail("upload should not have succeeded");
/*     */     }
/*     */     catch (ServiceException e) {}
/*     */     try {
/*  98 */       InputStream in = new ByteArrayInputStream(new byte['ஸ']);
/*  99 */       mbox.uploadContentAsStream("testLimitByFileUploadMaxSize2", in, "application/octet-stream", 3000L, 5000, true);
/*     */     } catch (SoapFaultException e) {
/* 101 */       fail("upload should not have failed");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMaxMessageSizeSaveDraft()
/*     */     throws Exception
/*     */   {
/* 111 */     setMaxMessageSize(2000L);
/*     */     
/*     */ 
/*     */ 
/* 115 */     Map<String, byte[]> attachments = new HashMap();
/* 116 */     attachments.put("file1.exe", new byte['Ϩ']);
/* 117 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 118 */     String aid = mbox.uploadAttachments(attachments, 5000);
/*     */     
/*     */ 
/* 121 */     ZMailbox.ZOutgoingMessage outgoing = new ZMailbox.ZOutgoingMessage();
/* 122 */     List<ZEmailAddress> addresses = new ArrayList();
/* 123 */     addresses.add(new ZEmailAddress(TestUtil.getAddress("user1"), null, null, "t"));
/*     */     
/* 125 */     outgoing.setAddresses(addresses);
/* 126 */     outgoing.setAttachmentUploadId(aid);
/* 127 */     String subject = NAME_PREFIX + " testMaxMessageSizeSaveDraft";
/* 128 */     outgoing.setSubject(subject);
/* 129 */     ZMessage draft = mbox.saveDraft(outgoing, null, null);
/*     */     
/*     */ 
/* 132 */     outgoing.setAttachmentUploadId(null);
/* 133 */     List<ZMailbox.ZOutgoingMessage.AttachedMessagePart> attachedParts = new ArrayList();
/* 134 */     attachedParts.add(new ZMailbox.ZOutgoingMessage.AttachedMessagePart(draft.getId(), "1", null));
/* 135 */     outgoing.setMessagePartsToAttach(attachedParts);
/* 136 */     mbox.sendMessage(outgoing, null, false);
/* 137 */     TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
/*     */     
/*     */ 
/* 140 */     setMaxMessageSize(1200L);
/*     */     try {
/* 142 */       mbox.sendMessage(outgoing, null, false);
/* 143 */       fail("Message send should not have succeeded.");
/*     */     }
/*     */     catch (SoapFaultException e) {
/* 146 */       validateMessageTooBigFault(e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testUploadMaxSize()
/*     */     throws Exception
/*     */   {
/* 156 */     setMaxMessageSize(900L);
/*     */     
/*     */ 
/* 159 */     Map<String, byte[]> attachments = new HashMap();
/* 160 */     attachments.put("file1.exe", new byte['Ϩ']);
/* 161 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     try {
/* 163 */       mbox.uploadAttachments(attachments, 5000);
/* 164 */       fail("Attachment upload should have failed");
/*     */     } catch (ZClientException e) {
/* 166 */       assertEquals("zclient.UPLOAD_SIZE_LIMIT_EXCEEDED", e.getCode());
/*     */     }
/*     */   }
/*     */   
/*     */   private void validateMessageTooBigFault(SoapFaultException e) throws Exception
/*     */   {
/* 172 */     Provisioning prov = Provisioning.getInstance();
/* 173 */     long maxSize = prov.getConfig().getLongAttr("zimbraMtaMaxMessageSize", -1L);
/* 174 */     assertTrue("Unexpected error: " + e.getMessage(), e.getMessage().matches("Message of size \\d+ exceeded allowed size"));
/*     */     
/* 176 */     assertEquals("mail.MESSAGE_TOO_BIG", e.getCode());
/* 177 */     assertEquals(Long.toString(maxSize), e.getArgumentValue("maxSize"));
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 182 */     cleanUp();
/* 183 */     TestUtil.setServerAttr("zimbraFileUploadMaxSize", this.mOrigFileUploadMaxSize);
/* 184 */     TestUtil.setConfigAttr("zimbraMtaMaxMessageSize", this.mOrigMaxMessageSize);
/*     */   }
/*     */   
/*     */   private void setMaxMessageSize(long numBytes) throws Exception
/*     */   {
/* 189 */     Provisioning prov = Provisioning.getInstance();
/* 190 */     Config config = prov.getConfig();
/* 191 */     Map<String, Object> attrs = new HashMap();
/* 192 */     attrs.put("zimbraMtaMaxMessageSize", Long.toString(numBytes));
/* 193 */     prov.modifyAttrs(config, attrs);
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 198 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 203 */     TestUtil.cliSetup();
/* 204 */     TestUtil.runTest(TestMaxMessageSize.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestMaxMessageSize.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */